home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14460 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  70 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help:what is wrong with this code?
  5. Date: 14 Apr 1996 18:56:23 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4ksac7INNk1g@keats.ugrad.cs.ubc.ca>
  8. References: <4krmdu$t3h@brahms.udel.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4krmdu$t3h@brahms.udel.edu>,
  12. Yue-hong Zheng <yuehong@brahms.udel.edu> wrote:
  13.  
  14. Sorry, I don't feel like helping you, but I will at least format the code
  15. for you so it has proper indentation and token spacing for clarity. It's the
  16. least I can do for a fellow programmer. I will do this by piping through the
  17. GNU indent program with the -kr option (guess what style _that_ is). 
  18.  
  19. #include <stdio.h>
  20.  
  21. void quicksort(int[], int, int);
  22.  
  23. void swap(int *, int *);
  24.  
  25. main()
  26. {
  27.     int k;
  28.     int a[] =
  29.     {1, 3, 4, 2, 43, 23, 5, 6, 87, 92, 21};
  30.     quicksort(a, 0, 10);
  31.     for (k = 0; k <= 10; k++) {
  32.     printf("%d\n", a[k]);
  33.     }
  34.     return 0;
  35. }
  36.  
  37. void swap(int *s, int *t)
  38. {
  39.     int tmp;
  40.     tmp = *s;
  41.     *s = *t;
  42.     *t = tmp;
  43. }
  44.  
  45. void quicksort(int array[], int left, int right)
  46. {
  47.     int i, j, median;
  48.     if (right <= left)
  49.     return;
  50.     median = (right + left) / 2;
  51.     swap(&array[median], &array[right]);
  52.     i = left;
  53.     j = right - 1;
  54.     while ((j - i) >= -1) {
  55.     while ((array[i] < array[right]) && (i <= (right - 1)))
  56.         i++;
  57.     while ((array[j] > array[right]) && (j >= 0))
  58.         j--;
  59.     swap(&array[i], &array[j]);
  60.     }
  61.     swap(&array[i], &array[right]);
  62.     quicksort(array, left, (i - 1));
  63.     quicksort(array, (i + 1), right);
  64. }
  65.  
  66.  
  67. Never post unindented code again unless it's unintended. Or vice versa.
  68. -- 
  69. I'm not really a jerk, but I play one on Usenet.
  70.